<?php

	/*
	* file: errorsManagerMod.php
	*
	* contents:	provides functions to check user input values posted from various forms
	*
	* author: Francesco Rubert, Federico Zanco (minor revs)
	*/



	/*
	* var: $docFieldsDesc
	*
	* contents:	keeps the type of table documents's fields. Types are:
	*			0 = int
	*			1 = char
	*			2 = text
	*			3 = date
	*			4 = medium blob
	*			fields order (and index) follows $docFieldsName order
	*/

	$docFieldsDesc =array(0,2,3,3,4,4,1,1,1,1,1,1,3,3);



	/*
	* var: $docFieldsDesc
	*
	* contents:	keeps the size (where meaningful) of table documents's fields
	*/

	$docFieldsSize =array(0,0,0,0,0,256,256,256,16,16,50,50,0,0);



	/*
	* function checkString($string, $length)
	*
	* Input required:	$string: string to check
	*					$length: length allowed for the field value
	*
	* Output:	returns an error if $string length is bigger than $length
	*
	* Author: Francesco Rubert
	*/

	function checkString($string, $length) {
		if (strlen($string) > $length) 
			return 'La stinga deve essere inferiore di '.$lenght .' caratteri ';
	}



	/*
	* function checkDateErr($date)
	*
	* Input required:	$date: a string with a date to check
	*
	* Output:	checks $date format. If $date is in not like DD/MM/YYYY returns an error
	*
	* Author: Francesco Rubert
	*/

	function checkDateErr($date) {

		//if $date is N/A it's not an error
		if ($date == "N/A")
			return '';
	
		//$error is the error message displayed
		$error ='La data inserita non &egrave; in formato GG/MM/AAAA oppure non &egrave; corretta.';
		
		//don't care about empty dates
		if (strlen($date) == 0)
			return '';
		
		$date = explode("/",$date);
		
		if (count($date)!= 3)
			return $error;

		//year must be in format YYYY.
		if (strlen($date[2]) != 4)
			return $error;
		
		//check for invalid values day bigger than 31 and month bigger than 12. Have your ever heard of a date like 89/45/2007? :)
		if ((intval($date[0]) > 31) || (intval($date[1]) > 12))
			return $error; 
		
		return '';
	}



	/*
	* function checkFileErr()
	*
	* Input required:	none
	*
	* Output:	checks if a file was uploaded. This is not the very correct way but it's easy...
	*
	* Author: Federico Zanco
	*/

	function checkFileErr() {
		if ($_FILES['userfile']['name'] != "")
			return "";
		else
			return "Campo obbligatorio.";
	}



	/*
	* function checkErrors($fieldsName, $fieldsDesc, $fieldsMap, $fieldsSize)
	*
	* Input required:	$fieldsName: array with names of the fields checked
	*					$fieldsDesc: array with the types of fields checked
	*					$fieldsMap: array with says which fields are gone to be checked. Values can be:
	*								0 = Don't check
	*								1 = check
	*								2 = mandatory (field must be not empty), check
	*					$fieldsSize: array with the size allowed for every field
	*
	* Output:	checks if a file was uploaded. This is not the very correct way but it's easy...
	*
	* Author: Francesco Rubert, Federico Zanco (minor revs)
	*/

	function checkErrors($fieldsName,$fieldsDesc,$fieldsMap,$fieldsSize) {

		//for every field...
		for ($i = 0; $i < count($fieldsMap); $i++) {
	
			$errors[$fieldsName[$i]]="";

			//get the value posted
			$_POST[$fieldsName[$i]] = trim($_POST[$fieldsName[$i]]);
	
			//if map says that a value for this field (value 2) is needed check if is empty
			if ($fieldsMap[$i] == 2 && $_POST[$fieldsName[$i]] == '') {
				$errors[$fieldsName[$i]] = 'Campo obbligatorio. ';
			} else {

				//if is not mandatory then if value is not empty check only if the value is correct
				if ($fieldsMap[$i] != 0 && $_POST[$fieldsName[$i]] != "") {
			
					switch ($fieldsDesc[$i]) {

						//check string values
						case 1:
							$errors[$fieldsName[$i]] = checkString($_POST[$fieldsName[$i]], $fieldsSize[$i]);
							break;

						//check date values
						case 3:
							$errors[$fieldsName[$i]] = checkDateErr($_POST[$fieldsName[$i]]);
							break;
					}

					//erase uncorrect values from $_POST. This becouse these values will'be used by UI
					if ( $errors[$fieldsName[$i]] != "")
						$_POST[$fieldsName[$i]] = "";
				} else
					//not mandatory empty fields... no errors!
					$errors[$fieldsName[$i]] = "";
			}
		}
		return $errors;
	}



	/*
	* function errors2Text($errors)
	*
	* Input required:	$errors: array of errors
	*
	* Output:	return a string with errors values ready to be printed in errors notification area
	*
	* Author: Federico Zanco
	*/

	function errors2Text($errors) {
	
		global $docFieldsName, $docFieldsNameLocale;
		
		for($i = 0; $i < count($errors); $i++) {
			if ($errors[$docFieldsName[$i]] != "")
				$errorsText = $errorsText . $docFieldsNameLocale[$i] . ": " .$errors[$docFieldsName[$i]]  . "<br />\n";
		}
		
		return $errorsText;
	}



	/*
	* function checkErrorsInsMod()
	*
	* Input required:	none
	*
	* Output:	call checkErrors with fieldsMap set for Insert values. Return a string with errors values 
	*
	* Author: Francesco Rubert, Federico Zanco (minor revs)
	*/

	function checkErrorsIns() {	
		global $docFieldsName, $docFieldsDesc, $docFieldsSize;
		
		$docFieldsMap = array(0,0,1,2,0,1,2,2,1,0,2,2,0,0);

		$errors = checkErrors($docFieldsName, $docFieldsDesc,$docFieldsMap,$docFieldsSize);
		$errors['filename'] = checkFileErr();
		
		return $errors;
	}



	/*
	* function checkErrorsMod()
	*
	* Input required:	none
	*
	* Output:	call checkErrors with fieldsMap set for Edit values. Return a string with errors values 
	*
	* Author: Francesco Rubert, Federico Zanco (minor revs)
	*/

	function checkErrorsMod() {
		global $docFieldsName, $docFieldsDesc, $docFieldsSize;
		
		$docFieldsMap = array(0,0,1,0,0,0,0,0,1,0,2,2);

		$errors = checkErrors($docFieldsName, $docFieldsDesc,$docFieldsMap,$docFieldsSize);

		return $errors;
	}


	/*
	* function checkErrorsSearch()
	*
	* Input required:	none
	*
	* Output:	call checkErrors with fieldsMap set for Search values. Return a string with errors values 
	*
	* Author: Francesco Rubert, Federico Zanco (minor revs)
	*/

	function checkErrorsSearch() {
		global $docFieldsName, $docFieldsDesc, $docFieldsSize;

		$docFieldsMap = array(0,0,0,0,0,0,1,1,1,1,1,1,1,1);
		
		return checkErrors($docFieldsName, $docFieldsDesc,$docFieldsMap,$docFieldsSize);
	}

?>
